﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Tradex.ComponentModel;
using Tradex.Market;

namespace Tradex.Trading {

    /// <summary>
    /// A PositionKey uniquely identifies a Position.
    /// </summary>
    [Serializable()]
    public sealed class PositionKey : Key<Position> {

        /// <summary>
        /// Constructor taking all the elements needed to construct the key.
        /// </summary>
        /// <param name="account"></param>
        /// <param name="instrument"></param>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1026:DefaultParametersShouldNotBeUsed")]
        public PositionKey(AccountKey account, InstrumentKey instrument = null) {
            Account = account;
            Instrument = instrument;
        }

        /// <summary>
        /// Gets the account the Position is on.
        /// </summary>
        public AccountKey Account { get; private set; }

        /// <summary>
        /// Gets the Instrument the position is for.
        /// </summary>
        public InstrumentKey Instrument { get; private set; }

        /// <inheritdoc />
        public override int GetHashCode() {
            return base.GetHashCode();
        }

        /// <inheritdoc />
        public override bool Equals(object obj) {
            PositionKey kObj = obj as PositionKey;
            if (kObj == null) {
                return false;
            }
            return kObj == this;
        }

        /// <inheritdoc />
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062")]
        public static bool operator ==(PositionKey left, PositionKey right) {
            if (Object.ReferenceEquals(left, right)) {
                return true;
            }
            if (Object.ReferenceEquals(right, null)) {
                return false;
            }
            if (left.Account != right.Account) {
                return false;
            }
            if (left.Instrument != right.Instrument) {
                return false;
            }
            return true;
        }

        /// <inheritdoc />
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1062")]
        public static bool operator !=(PositionKey left, PositionKey right) {
            if (Object.ReferenceEquals(left, right)) {
                return true;
            }
            if (Object.ReferenceEquals(right, null)) {
                return false;
            }
            if (left.Instrument != right.Instrument) {
                return true;
            }
            if (left.Account != right.Account) {
                return true;
            }
            if (left.Instrument != right.Instrument) {
                return true;
            }
            return false;
        }

    }

}
